﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using QTMusic.BaseClasses;

namespace QTMusic.UserControls
{
    public partial class ShareASong : System.Web.UI.UserControl
    {
        public string SongToShare = "";
        public string artistName = "";
        public string genreName = "";
        public string SongUpTime = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LstShareOption.Items.Add(new ListItem("public", "2"));
                LstShareOption.Items.Add(new ListItem("friend", "1"));
                LstShareOption.Items.Add(new ListItem("private", "0"));

                int SongToShareId = 0;
                int temp;
                if (int.TryParse(Request.QueryString["SongId"], out temp))
                    SongToShareId = temp;

                SqlConnection conn = new SqlConnection(Common.connString);
                SqlCommand sql = new SqlCommand("SELECT s.song_name, a.artist_name, g.genre_name, su.songup_time  " +
                                                "FROM   song AS s INNER JOIN " +
                                                "artist AS a ON s.artist_id = a.artist_id INNER JOIN " +
                                                "genre AS g ON s.genre_id = g.genre_id INNER JOIN " +
                                                "songup AS su ON s.song_id = su.song_id " +
                                                "WHERE s.song_id = @songId", conn);

                sql.Parameters.AddWithValue("@songId", SongToShareId);

                conn.Open();
                SqlDataReader reader = sql.ExecuteReader();
                reader.Read();
                SongToShare = (string)reader["song_name"];
                artistName = (string)reader["artist_name"];
                genreName = (string)reader["genre_name"];
                SongUpTime = ((DateTime)reader["songup_time"]).ToShortDateString();
                conn.Close();

                this.DataBind();
            }
        }

        protected void BtnShareClick(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["QTCookie"];
            if (!(cookie != null && cookie["Name"] != null && cookie["ID"] != null))
                Response.Redirect("~/Websites/Default.aspx");

            int temp;
            int CurrentUserId = 0;
            int SongToShareId = 0;
            if (int.TryParse(cookie["ID"], out temp))
                CurrentUserId = temp;
            if (int.TryParse(Request.QueryString["SongId"], out temp))
                SongToShareId = temp;
            string CurrentTime = DateTime.Now.ToString();

            SqlConnection conn = new SqlConnection(Common.connString);
            // Then insert new song record
            SqlCommand sql = new SqlCommand("INSERT INTO [share] (user_id, song_id, share_time, share_permission, share_description) VALUES (@user_id, @song_id, @share_time, @share_permission, @description)", conn);
            
            sql.Parameters.AddWithValue("@user_id", CurrentUserId);
            sql.Parameters.AddWithValue("@song_id", SongToShareId);
            sql.Parameters.AddWithValue("@share_time", CurrentTime);
            sql.Parameters.AddWithValue("@share_permission", int.Parse(LstShareOption.SelectedValue));
            sql.Parameters.AddWithValue("@description", TextBox1.Text);
            
            conn.Open();
            int result = sql.ExecuteNonQuery();
            conn.Close();

            if (result != 0)
            {
                Label1.Text = "inserted";
                
                sql = new SqlCommand("SELECT max(share_id) AS last_id FROM [share]", conn);

                conn.Open();
                SqlDataReader reader = sql.ExecuteReader();
                reader.Read();
                int newlyInsertedShareId = (int)reader["last_id"];
                conn.Close();


                sql = new SqlCommand("INSERT INTO [activity] (user_id, share_id, permission, time) VALUES (@user_id, @share_id, @permission, @share_time)", conn);

                sql.Parameters.AddWithValue("@user_id", CurrentUserId);
                sql.Parameters.AddWithValue("@share_id", newlyInsertedShareId);
                sql.Parameters.AddWithValue("@permission", int.Parse(LstShareOption.SelectedValue));
                sql.Parameters.AddWithValue("@share_time", CurrentTime);

                conn.Open();
                result = sql.ExecuteNonQuery();
                conn.Close();

                if (result != 0)
                {
                    Label1.Text = "success";
                    Response.Redirect("~/Websites/Profile.aspx?id=" + CurrentUserId);
                }
                else
                    Label1.Text = "failed";
            }
            else
            {
                Label1.Text = "not inserted";
            }
        }
    }
}